/* 
 * Spring mixin mybatis frame code general
 * Fu(iknoweth@yahoo.com) All rights reserved 
 * 
 */
package com.pricl.biz.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pricl.biz.system.entity.SystemCode;
import com.pricl.biz.system.service.SystemCodeService;
import com.pricl.biz.system.vo.CodeForm;
import com.pricl.frame.common.module.TreeNodeStr;
import com.pricl.frame.common.module.ValueText;
import com.pricl.frame.core.R;
import com.pricl.frame.core.User;

/**
 * <p>
 * <strong>码表</strong>Access Controller
 * </p>
 *
 */
@RequestMapping("/system/code")
@Controller
public class SystemCodeController {

	/** 系统-字典数据访问接口 **/
	@Autowired
	SystemCodeService service;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {
		return "/system/code/index";
	}

	/**
	 * <p>
	 * 获取字典类型列表
	 * </p>
	 * 
	 * @param id
	 * @return List
	 */
	@RequestMapping(value = "/treeType", method = RequestMethod.POST)
	public @ResponseBody List<TreeNodeStr> treeType(String id) {
		return service.treeType(id);
	}

	/**
	 * <p>
	 * 获取字典列表
	 * </p>
	 * 
	 * @param type
	 * @return List
	 */
	@RequestMapping(value = "/findCodes", method = RequestMethod.POST)
	public @ResponseBody List<SystemCode> findCodes(String type) {
		return service.findCodes(type);
	}

	/**
	 * <p>
	 * 保存码表信息
	 * </p>
	 * 
	 * @param form
	 * @param user
	 * @return R
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody R save(CodeForm form, User user) {
		service.save(form, user);
		return R.ok();
	}

	/**
	 * <p>
	 * 根据类型获取列表
	 * </p>
	 * 
	 * @param type
	 * @return List
	 */
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	public @ResponseBody List<SystemCode> query(String type) {
		return service.findByType(type);
	}

	/**
	 * <p>
	 * 根据类型获取列表
	 * </p>
	 * 
	 * @param type
	 * @return List
	 */
	@RequestMapping(value = "/list/{type}", method = RequestMethod.POST)
	public @ResponseBody List<ValueText> list(@PathVariable String type) {
		return service.list(type);
	}

	/**
	 * <p>
	 * 加载码表，若有新版本则加载，否则不加载
	 * </p>
	 * 
	 * @param version
	 * @return R
	 */
	@RequestMapping(value = "/loadValid", method = RequestMethod.POST)
	public @ResponseBody R loadValid(Integer version) {
		return new R(1, service.findAllValid(), "");
	}

	/**
	 * <p>
	 * 根据类型获取列表
	 * </p>
	 * 
	 * @param type
	 * @return Map
	 */
	@RequestMapping(value = "/listByTypes", method = RequestMethod.POST)
	public @ResponseBody Map<String, List<ValueText>> listByTypes(String[] type) {
		if (type == null || type.length == 0) {
			return new HashMap<String, List<ValueText>>();
		}
		Map<String, List<ValueText>> codes = new HashMap<String, List<ValueText>>();
		for (String t : type) {
			codes.put(t, service.list(t));
		}
		return codes;
	}
}
